

	var shindig = shindig || {};
	shindig.samplecontainer = {};

	var parentUrl = document.location.href;
	var baseUrl = parentUrl.substring(0, parentUrl.indexOf('samplecontainer.html'));
	
	var socialDataPath = document.location.protocol + "//" + document.location.host
	+ "/social/rest/samplecontainer/";
	
	var gadget;
	var viewerId = "john.doe";
	var ownerId = "john.doe";
	
	var viewMatches = /[?&]view=((?:[^#&]+|&amp;)+)/.exec(parentUrl);
	var current_view = (viewMatches) ? viewMatches[1] : "default";
	
	// Holds an array of data describing the gadgets to be rendered
	var gadgetData;
	
	function init_shindig() {
		var shindig = shindig || {};
		shindig.samplecontainer = {};
		gadgets.container.nextGadgetInstanceId_ = 0;
	}
  
	
	/*
	 * Requests the metadata for the list of gadgets contained in gadgetData
	 * 
	 * Successful metadata request results in a call to generateGadgets which
	 * will generate the gadgets for display.
	 * 
     * gadget_data is an array like the sample below:
     * 		gadget_data = [
     * 						{
	 *							url: spec_url, 
	 *							container_id: container_id,
	 *							moduleId: 1 },
     * 						{
	 *							url: spec_url, 
	 *							container_id: container_id,
	 *							moduleId: 1 },	
     * 					];
	 */
	function requestGadgetMetaData(gadget_data) {
		gadgetData = gadget_data;
		var request = {
			context: {
				country: "default",
				language: "default",
				view: current_view,
				container: "default"
			},
			gadgets: gadgetData
		};
		sendRequestToServer(gadgetServer + "/gadgets/metadata", 
							"POST",
							gadgets.json.stringify(request), 
							generateGadgets, 
							true);		
	}

	function generateSecureToken(gadget_url) {
		var appId = 0;
		for (var i = 0; i < gadget_url.length; i++) {
			appId += gadget_url.charCodeAt(i);
		}
		var fields = [ownerId, viewerId, appId, "shindig", gadget_url, "0", "default"];
		for (var i = 0; i < fields.length; i++) {
			// escape each field individually, for metachars in URL
			fields[i] = escape(fields[i]);
		}
		return fields.join(":");
	}

	SampleContainerGadget = function(opt_params) {
		gadgets.IfrGadget.call(this, opt_params);
	};
	
	SampleContainerGadget.inherits(gadgets.IfrGadget);
	gadgets.container.gadgetClass = SampleContainerGadget;

	/*
	 * Sends a request to the Gadget server.
	 */
	function sendRequestToServer(url, method, opt_postParams, opt_callback, opt_excludeSecurityToken) {
		opt_postParams = opt_postParams || {};
		var makeRequestParams = {
				"CONTENT_TYPE" : "JSON",
				"METHOD" : method,
				"POST_DATA" : opt_postParams};
		
		if (!opt_excludeSecurityToken) {
			url = socialDataPath + url + "?st=" + gadget.secureToken;
		}
		gadgets.io.makeNonProxiedRequest(url,
			function(data) {
				data = data.data;
				if (opt_callback) {
					opt_callback(data);
				}
			},
			makeRequestParams
		);
	};

	function generateGadgets(metadata) {
	    gadgets.container.view_ = current_view;
	    gadgets.container.gadgets_ = {};
	    for (var i = 0; i < metadata.gadgets.length; i++) {
	      gadget = gadgets.container.createGadget({'specUrl': metadata.gadgets[i].url, 
		  										   'width': metadata.gadgets[i].width,
												   'height': metadata.gadgets[i].height,
		  										   'title': metadata.gadgets[i].title});
	      gadget.secureToken = escape(generateSecureToken(metadata.gadgets[i].url));
	      gadgets.container.addGadget(gadget);
	    }
		var container_ids = [];
		for (i = 0; i < gadgetData.length; i++) {
			container_ids.push(gadgetData[i].container_id);
		}
    	gadgets.container.layoutManager.setGadgetChromeIds(container_ids);
		gadgets.container.renderGadgets();
  	};

	function refreshGadgets(metadata) {
		for (var gadget in gadgets.container.gadgets_) {
			var newtitle = metadata.gadgets[0].title;
			var specUrl = metadata.gadgets[0].url;
			gadgets.container.gadgets_[gadget].title = newtitle;
			gadgets.container.gadgets_[gadget].specUrl = specUrl;
			gadgets.container.gadgets_[gadget].secureToken = escape(generateSecureToken());
		}
		gadgets.container.renderGadgets();
	}


    function dumpObj(obj, name, indent, depth){
		var MAX_DUMP_DEPTH = 10;
        if (depth > MAX_DUMP_DEPTH) {
            return indent + name + ": <Maximum Depth Reached>\n";
        }
        if (typeof obj == "object") {       
            var child = null;          
            var output = indent + name + "\n";      
            indent += "\t";
            for (var item in obj) {
                try {
                    child = obj[item];
                } 
                catch (e) {
                    child = "<Unable to Evaluate>"; 
                }
                
                if (typeof child == "object") {
                    output += dumpObj(child, item, indent, depth + 1);
                }
                else {
                    output += indent + item + ": " + child + "\n";
                } 
            }
            return output;
        }
        else {
            return obj;
        }
    }




